package org.vectrics.recruiter.struts.admin;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.vectrics.common.util.FileUtil;
import org.vectrics.struts.Request;

/**
 * @author mmoore
 */
public class UploadResumeForm extends ActionForm {

	private static final long serialVersionUID = 8783788277281566266L;
	
	private String userId = null;
	private FormFile theFile = null;
	private String name = null;


	/**
	 * @return Returns the userId.
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * @param userId The userId to set.
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * @return Returns the theFile.
	 */
	public FormFile getTheFile() {
		return theFile;
	}

	/**
	 * @param theFile The theFile to set.
	 */
	public void setTheFile(FormFile theFile) {
		this.theFile = theFile;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		// Si presiona cancelar no valido nada.
		if (request.getParameter("cancel") != null) {
			return errors;
		}
		
		// Valido que el file no sea nulo 
		if ((this.getTheFile()==null)||(this.getTheFile().getFileSize()==0)) {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.file.required"));
		} else {			
			// Valido el que el size no sea mayor de 60M 
			if (this.getTheFile().getFileSize() > 60000000) {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.file.size.max"));
			}
			
			String fileName = this.getTheFile().getFileName();
			// Se valida el tipo de archivo
			if (fileName.endsWith("pdf")) {
			}
			else if (fileName.endsWith("txt")) {
			}
			else if (fileName.endsWith("doc")) {
			}
			else {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.file.type"));
			}
		}
		
		// Valido que el nombre 
		if ((this.getName() == null)||(this.getName().trim().equals(""))) {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required.name"));
		} else {
			if (this.getName().length() > 50) {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.name.max.length"));
			} else {
				if (!FileUtil.isValidName(this.getName())) {
					errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.name.is.invalid"));
				}
			}
		}
		
		return errors;
	}

	protected boolean wasButtonPressed(Request request, String name) {
	    boolean result = false;
	    String paramValue = request.getParameter(name);
	    if (paramValue == null) {
	        paramValue = request.getParameter(name + ".x");
	    }
	    if (paramValue != null)
	        result = true;
	    return(result);
	}	
	
}
